package com.commerce.controller.back.ad;

import com.commerce.authority.AuthType;
import com.commerce.authority.Authority;
import com.commerce.common.JsonResult;
import com.commerce.common.Reminder;
import com.commerce.model.AD;
import com.commerce.page.ADPage;
import com.commerce.page.Page;
import com.commerce.service.ADService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 广告控制器
 */
@Controller
@RequestMapping(value = "/admin/ads" , method = RequestMethod.POST)
public class ADController {

    @Autowired
    private ADService adService;

    /**
     * 列表页面
     * @return
     */
    @RequestMapping("list")
    @Authority(AuthType.AD_SELECT)
    public String list(){
        return "back/ad/list";
    }

    /**
     * 分页查询
     * @param adPage
     * @return
     */
    @ResponseBody
    @RequestMapping("pageList")
    public JsonResult pageList(ADPage adPage){
        adService.selectByPage(adPage);
        return JsonResult.returnJsonResult("0","",adPage.getTotal()+"",adPage.getList());
    }

    /**
     * 新增或修改页面
     * @param id
     * @param model
     * @return String
     */
    @RequestMapping("info")
    @Authority({AuthType.AD_INSERT,AuthType.AD_UPDATE})
    public String info(Integer id , Model model){
        AD ad = null;
        if( id != null && id>0){
            ad = adService.selectById(id);
        }
        model.addAttribute("ad" , ad);
        return "back/ad/info";
    }

    /**
     * 新增
     * @param ad
     * @param request
     * @return String
     */
    @ResponseBody
    @RequestMapping("/addAD")
    @Authority(AuthType.AD_INSERT)
    public String addAD(AD ad , HttpServletRequest request){
        return adService.insert(ad,request);
    }

    /**
     * 修改
     * @param ad
     * @param request
     * @return String
     */
    @ResponseBody
    @RequestMapping("/editAD")
    @Authority(AuthType.AD_UPDATE)
    public String editAD(AD ad , HttpServletRequest request){
        return adService.update(ad,request);
    }

    /**
     * 开启与关闭
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateByOpen")
    @Authority(AuthType.AD_UPDATE)
    public String updateByOpen(int id){
        return adService.updateByOpen(id);
    }

    /**
     * 删除广告
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delAD")
    @Authority(AuthType.AD_DELETE)
    public String delAD(int id){
        return adService.deleteById(id);
    }
}
